package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getProperty;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetPropertyByPostgresFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String tableName = (String) flowContext.checkData("tableName");
        String columnName = (String) flowContext.checkData("columnName");
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段信息")
                .putTemporaryData("sql", "select attname as column_name, attnum as oridinal_position, attnotnull as notnull, format_type(atttypid,atttypmod) as type, col_description(attrelid, attnum) as comment from pg_attribute join pg_class on pg_attribute.attrelid = pg_class.oid where attnum > 0 and atttypid > 0 and pg_class.relname = ? and attname = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            Property property = new Property();
                            property.column = resultSet.getString("column_name");
                            property.columnType = resultSet.getString("type");
                            property.notNull = "t".equals(resultSet.getString("notnull"));
                            property.comment = resultSet.getString("comment");
                            flowContext.putData("property", property);
                        }
                    }
                })
                .execute();

        if(!flowContext.containKey("property")){
            flowContext.broken("字段不存在");
        }
        Property property = (Property) flowContext.checkData("property");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段类型信息")
                .putTemporaryData("sql", "select ordinal_position,column_default,is_nullable,udt_name,column_default from information_schema.columns where table_name = ? and column_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            property.columnType = resultSet.getString("udt_name");
                            if (null != resultSet.getString("column_default")) {
                                property.defaultValue = resultSet.getString("column_default");
                                if(property.defaultValue.startsWith("nextval(")){
                                    property.id = true;
                                    property.strategy = IdStrategy.AutoIncrement;
                                }
                            }
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库字段-Postgres数据库";
    }
}
